<template>
  <div class="param-main">
    <el-form ref="tblMain" :model="tblMain" labelPosition="right" inline="" label-suffix=":" label-width="100px">
      <el-row :span="24">
        <el-col :span="6">
          <el-form-item label="门店名称">
            <el-input v-model="tblMain.param.orgName" size="small" placeholder=""></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="所属区域">
            <el-cascader v-model="tblMain.param.regionCodePath" size="small" :props="props" style="width: 100%" />
          </el-form-item>
        </el-col>
      </el-row>
      <div class="page-pane2">
        <el-button type="primary" size="small" @click="initTableData()">查询</el-button>
        <el-button size="small" @click="dataReset()">清空</el-button>
        <el-button size="small" type="info" @click="handleAdd()">新增</el-button>
        <!-- <el-button size="small" type="danger" @click="deleteEvent()">删除</el-button> -->
      </div>
    </el-form>
    <div>
      <el-table ref="table" row-key="id" style="margin-top: 10px" :data="tblMain.items" v-loading.body="tblMain.loading"
        border stripe lazy :load="load" :tree-props="{ children: 'children', hasChildren: 'hasChildren' }">
        <el-table-column prop="orgName" width="200" label="门店名称" align="left" show-overflow-tooltip></el-table-column>
        <el-table-column prop="isManager" width="110" label="是否为主门店" align="center" show-overflow-tooltip>
          <template slot-scope="scope">
            <span v-if="scope.row.isManager == '1'">是</span>
            <span v-else-if="scope.row.isManager == '0'">否</span>
            <span v-else></span>
          </template>
        </el-table-column>
        <el-table-column prop="regionName" width="300" label="所属区域" align="center"
          show-overflow-tooltip></el-table-column>
        <el-table-column prop="managerAccount" width="150" label="管理员账号" align="center"
          show-overflow-tooltip></el-table-column>
        <el-table-column prop="contacts" label="联系人" align="center" show-overflow-tooltip></el-table-column>
        <el-table-column prop="phoneNum" width="200" label="联系电话" align="center" show-overflow-tooltip></el-table-column>
        <el-table-column label="操作" align="center" width="180px">
          <template slot-scope="scope">
            <el-button type="text" @click="handleEdit(scope.row)">编辑</el-button>
            <el-divider direction="vertical"></el-divider>
            <el-button type="text" @click="deleteRow(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination style="margin-top: 10px" :current-page.sync="tblMain.currentPage" :page-size="tblMain.pageSize"
        :total="tblMain.total" layout="total, sizes, prev, pager, next" @size-change="handleSizeChange"
        @current-change="handleCurrentChange"></el-pagination>
    </div>
    <store-dialog ref="modalForm" @ok="modalFormOk"></store-dialog>
  </div>
</template>
<script>
import StoreDialog from './components/StoreDialog'
import tool from '@/tool'
import {
  sysRegionListByParentCode,
  sysOrganizationList,
  sysOrganizationDel,
  sysOrganizationListByParentId
} from '@/api/index'
export default {
  name: 'StoreList',
  components: {
    StoreDialog
  },
  data () {
    return {
      tblMain: tool.genTableData({
        param: {
          orgName: '',
          regionCodePath: null
        },
        items: []
      }),
      props: {
        lazy: true,
        checkStrictly: true,
        lazyLoad: (node, resolve) => {
          setTimeout(async () => {
            const res = await this.getTreeByParentCode(node)
            return resolve(res)
          })
        }
      }
    }
  },
  mounted () {
    this.initTableData()
  },
  methods: {
    getTreeByParentCode (node) {
      return new Promise((resolve, reject) => {
        let params = {}
        if (node.data) {
          params = { parentCode: node.data.value }
        }
        sysRegionListByParentCode(params).then((res) => {
          if (res.data.code == 0) {
            const nodes = Array.from(res.data.data).map((item) => ({
              value: item.code,
              label: item.name,
              leaf: item.type == 'street'
            }))
            resolve(nodes)
          }
        })
      })
    },
    handleAdd () {
      this.$refs.modalForm.add()
    },
    handleEdit (val) {
      this.$refs.modalForm.edit(val)
    },
    load (tree, treeNode, resolve) {
      sysOrganizationListByParentId({ parentId: tree.id }).then((res) => {
        let data = res.data
        if (data && data.code == 0) {
          resolve(data.data)
        }
      })
    },
    resetLazyTree () {
      this.$set(this.$refs.table.store.states, 'lazyTreeNodeMap', {})
      this.$set(this.$refs.table.store.states, 'treeData', {})
      this.$set(this.$refs.table.store.states, 'data', [])
    },
    modalFormOk () {
      this.initTableData()
    },
    // 单个删除
    async deleteRow (val) {
      this.$confirm('确定删除该数据?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          let params = []
          params.push(val.id)
          await this.deleteTblMain(params)
        })
        .catch(() => { })
    },
    deleteTblMain (params) {
      let me = this
      me.tblMain.loading = true
      sysOrganizationDel(params)
        .then((res) => {
          me.tblMain.loading = false
          let data = res.data
          if (data && data.code == 0) {
            me.$message.success('删除成功')
            this.initTableData()
          }
        })
        .catch(() => {
          me.$message.error('请求失败')
          me.tblMain.loading = false
        })
    },
    handleSelectionChange (val) {
      this.tblMain.selection = val
    },
    handleSizeChange (val) {
      this.tblMain.pageSize = val
      this.initTableData()
    },
    handleCurrentChange (val) {
      this.tblMain.currentPage = val
      this.initTableData()
    },
    dataReset () {
      this.tblMain.currentPage = 1
      this.tblMain.pageSize = 10
      this.tblMain.param.orgName = ''
      this.tblMain.param.regionCodePath = null
      this.initTableData()
    },
    initTableData () {
      let me = this
      this.resetLazyTree()
      let param = {
        orgName: me.tblMain.param.orgName,
        regionCode: me.tblMain.param.regionCodePath
          ? me.tblMain.param.regionCodePath[
          me.tblMain.param.regionCodePath.length - 1
          ]
          : '',
        page: me.tblMain.currentPage,
        size: me.tblMain.pageSize
      }
      me.tblMain.loading = true
      sysOrganizationList(param)
        .then((res) => {
          me.tblMain.loading = false
          let data = res.data
          if (data && data.code == 0) {
            me.tblMain.items = data.data.record
            me.tblMain.total = data.data.total
          }
        })
        .catch(() => {
          // me.$message.error('请求失败')
          me.tblMain.loading = false
        })
    }
  }
}
</script>
